package cn.xinfei.xdecision.engine.runner.service;

import cn.xinfei.xdecision.common.datamodel.DecisionReentryLog;
import cn.xinfei.xdecision.common.datamodel.DecisionResult;
import cn.xinfei.xdecision.common.utils.util.JsonUtils;
import cn.xinfei.xdecision.constant.EngineResultEnum;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.context.XDecisionContext;
import cn.xinfei.xdecision.engine.runner.context.model.NodeContextModel;
import cn.xinfei.xdecision.engine.runner.producer.DecisionProducer;
import cn.xinfei.xdecision.engine.runner.vo.EngineApiResponse;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class EngineReentryLogService {

    @Autowired
    private DecisionReentryLogService decisionReentryLogService;

    @Autowired
    private DecisionResultService decisionResultService;

    @Autowired
    private DecisionResultDetailService decisionResultDetailService;

    @Autowired
    private DecisionProducer decisionProducer;

    public DecisionReentryLog saveDecisionReentryLog(String scheduleModel) {
        String bizId = PipelineContextHolder.getRequestId();
        String decisionId = PipelineContextHolder.getDecisionId();
        DecisionReentryLog decisionReentryLog = new DecisionReentryLog();
        try {
            decisionReentryLog.setRequestId(bizId);
            decisionReentryLog.setDecisionId(decisionId);
            decisionReentryLog.setEngineCode(PipelineContextHolder.getEngineCode());
            decisionReentryLog.setEngineName(PipelineContextHolder.getEngineName());
            decisionReentryLog.setEngineVersion(PipelineContextHolder.getEngineVersion());
            decisionReentryLog.setScene(PipelineContextHolder.getEngineVersion());
            decisionReentryLog.setScheduleModel(scheduleModel);
            decisionReentryLogService.saveDecisionReentryLogByBizId(decisionReentryLog);
        } catch (Exception e) {
            log.error("save decisionReentryLog fail.msg={},log={}", e.getMessage(), JsonUtils.toJsonString(decisionReentryLog), e);
        }
        return decisionReentryLog;
    }



    public void updateDecisionResult(EngineApiResponse currResponse, String executorDecisionId,String updateDecisionId, String engineCode ) {
        DecisionResult decisionResult = new DecisionResult();
        try {
            String currResponseCode = currResponse.getCode();
            EngineResultEnum engineResultEnum = EngineResultEnum.getByCode(currResponseCode);
            XDecisionContext context = XDecisionContext.getContext();
            decisionResult.setDecisionId(updateDecisionId);//update
//            decisionResult.setScene(context.getTpCode());
            decisionResult.setScene(PipelineContextHolder.getScene());
            decisionResult.setState(engineResultEnum.getState());
            decisionResult.setResult(engineResultEnum.getResult());
            decisionResult.setCode(engineResultEnum.getCode());
            decisionResult.setRespond(currResponse.getRespond());
            decisionResult.setEngineCode(engineCode);

            //更新状态
            decisionResultService.upsertDecisionResultByDecisionId(decisionResult,executorDecisionId,updateDecisionId);


        } catch (Exception e) {
            log.error("updateDecisionResultStateLog error={},decisionResult={}", e.getMessage(), JsonUtils.toJsonString(decisionResult), e);
        }

    }

}
